package com.metaera.app.ui.popup;

import android.content.Context;
import android.graphics.Color;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.lxj.xpopup.impl.PartShadowPopupView;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.CalendarPresenter;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.ui.fragments.FlashNewsCalendarItemFragment;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import androidx.fragment.app.Fragment;

/**
 * 日历
 */
public class CalendarPopup extends PartShadowPopupView implements INetworkRequestView<List<Date>> {

    private CalendarView mCalendarView;

    private CalendarPresenter mPresenter;

    private int mCategoryID, mCurrentYear, mCurrentMonth;

    //    private final Map<String, Calendar> map = new HashMap<>();
    private Fragment mFragment;

    public CalendarPopup(Context context) {
        super(context);
    }

    public CalendarPopup(Fragment fragment, CalendarPresenter presenter, int categoryID) {
        super(fragment.getContext());
        mFragment = fragment;
        mPresenter = presenter;
        mCategoryID = categoryID;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_flash_news_calendar;
    }

    public void setCategoryID(int categoryID) {

        mCategoryID = categoryID;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        ImageView ivLeft = findViewById(R.id.iv_left);
        TextView tvTitle = findViewById(R.id.tv_title);
        ImageView ivRight = findViewById(R.id.iv_right);
        Button buttonConfirm = findViewById(R.id.button_confirm);
        mCalendarView = findViewById(R.id.calendar_view);
        Button button_back_today = findViewById(R.id.button_back_today);

        tvTitle.setText(String.format(
                getContext().getResources().getString(R.string.calendar_placeholder),
                mCalendarView.getCurYear(), mCalendarView.getCurMonth())
        );

        ivLeft.setOnClickListener(v -> mCalendarView.scrollToPre(true)); // 上一月
        ivRight.setOnClickListener(v -> mCalendarView.scrollToNext(true)); // 下一月
        button_back_today.setOnClickListener(v -> mCalendarView.scrollToCurrent()); // 当前

        mCurrentYear = mCalendarView.getCurYear();
        mCurrentMonth = mCalendarView.getCurMonth();

        mCalendarView.setOnMonthChangeListener((y, m) -> { // 监听月份变动
            mCurrentYear = y;
            mCurrentMonth = m;
            tvTitle.setText(String.format(getContext().getResources().getString(R.string.calendar_placeholder), y, m));
            mPresenter.execute(String.format(Locale.getDefault(), "%1$d-%2$d", y, m), mCategoryID);
        });

        buttonConfirm.setOnClickListener(v -> {

            // 调用子fragment
            List<Fragment> fragments = mFragment.getChildFragmentManager().getFragments();
            for (Fragment fragment : fragments) {
                String dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
                        .format(new Date(mCalendarView.getSelectedCalendar().getTimeInMillis()));
                ((FlashNewsCalendarItemFragment) fragment).set(dateFormat);
            }

            // 消失
            dismiss();
        });

    }

    @Override
    protected void doAfterShow() {
        super.doAfterShow();
        mCalendarView.clearSchemeDate();
        String dateFormat = String.format(Locale.getDefault(), "%1$d-%2$d", mCurrentYear, mCurrentMonth);
        mPresenter.execute(dateFormat, mCategoryID);
    }

    @Override
    public void successful(List<Date> data) {
        Map<String, Calendar> map = new HashMap<>();

        for (Date date : data) {
            java.util.Calendar instance = java.util.Calendar.getInstance();
            instance.setTime(date);
            Calendar calendar = new Calendar();
            calendar.setYear(instance.get(java.util.Calendar.YEAR));
            calendar.setMonth(instance.get(java.util.Calendar.MONTH) + 1);
            calendar.setDay(instance.get(java.util.Calendar.DAY_OF_MONTH));
            calendar.setSchemeColor(Color.parseColor("#FF0822B1"));
            map.put(calendar.toString(), calendar);
        }

        mCalendarView.setSchemeDate(map);
    }

}
